package org.peekmoon.meteon;

import javax.media.opengl.DebugGL2;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.TraceGL2;

import com.jogamp.newt.event.MouseEvent;
import com.jogamp.newt.event.MouseListener;

public class MainEventListener implements GLEventListener, MouseListener {
	
	private double rotX = 0;
	private double rotY = 0;
	
	

	public void init(GLAutoDrawable glad) {
        GL2 gl;
        gl = new TraceGL2(new DebugGL2(glad.getGL().getGL2()), System.out);
        gl.glShadeModel(GL2.GL_SMOOTH);
	}
	
	public void display(GLAutoDrawable glad) {
		GL2 gl = glad.getGL().getGL2();
        //gl = new TraceGL2(new DebugGL2(glad.getGL().getGL2()), System.out);
        gl.glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        
        gl.glLoadIdentity();
        gl.glTranslated(-1.5, 0.0, -6.0);
        
        gl.glRotated(rotY, 0, 1, 0);
        gl.glRotated(rotX, 1, 0, 0);
        
        gl.glBegin(GL2.GL_TRIANGLES);
        	gl.glColor3d(0.0, 0.0, 1.0);
        	gl.glVertex3d( 0.0, 1.0, 0.0);  // Top
        	gl.glColor3d(0.0, 1.0, 0.0);
        	gl.glVertex3d(-1.0,-1.0, 0.0);  // Bottom Left
        	gl.glColor3d(1.0, 0.0, 0.0);
        	gl.glVertex3d( 1.0,-1.0, 0.0);  // Bottom Right
        gl.glEnd();
        
        gl.glTranslated(3.0, 0.0, 0.0);
        
        gl.glBegin(GL2.GL_QUADS);              // Draw A Quad
        	gl.glColor3d(0.6, 0.2, 0.2);
        	gl.glVertex3d(-1.0, 1.0, 0.0);     // Top Left
        	gl.glVertex3d( 1.0, 1.0, 0.0);     // Top Right
        	gl.glVertex3d( 1.0,-1.0, 0.0);     // Bottom Right
        	gl.glVertex3d(-1.0,-1.0, 0.0);     // Bottom Left
        gl.glEnd();                            // Done Drawing The Quad
		
	}

	public void dispose(GLAutoDrawable glad) {
	}


    public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) {
    	GL2 gl = new TraceGL2(new DebugGL2(glad.getGL().getGL2()), System.out);
    	gl.glViewport(x, y, width, height);
    	gl.glMatrixMode(GL2.GL_PROJECTION);
    	gl.glLoadIdentity();
    	
    	double fovY = 45.0;
    	double aspect = (double)width / height;
    	double zNear = 0.1;
    	double zFar = 100;
    	double fH = Math.tan( (fovY / 2) / 180 * Math.PI) *zNear;
    	double fW = fH * aspect;
    	
    	
    	gl.glFrustum(-fW, fW, -fH, fH, zNear, zFar);
    	
    	gl.glMatrixMode(GL2.GL_MODELVIEW);
    	gl.glLoadIdentity();
    	
    }
    
	private double lastX;
	private double lastY;

	public void mouseClicked(MouseEvent e) {
		System.out.println("mouse clicked");
	}


	public void mouseDragged(MouseEvent e) {
		rotY += e.getX() - lastX;
		rotX += e.getY() - lastY;
		lastX = e.getX();
		lastY = e.getY();
		
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseMoved(MouseEvent e) {

		
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("mouse pressed");
		lastX = e.getX();
		lastY = e.getY();		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseWheelMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
